package com.study.talk.net;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttp {

    private static final String TAG = "OkHttp";
    //public static String IP = "101.37.26.61";  //可以设置为自己的服务器IP，
    public static String IP = "192.168.137.22"; //本机测试的话localhost也可以

    public static Integer NETTY_PORT = 10001; //端口记得和SpringBoot后端保持一致
    public static Integer WEB_PORT = 10002;  //Web后端端口

    public static String URL = "http://" + IP + ":" + WEB_PORT;
    public static String token = "";

    public static void get(String url, Then then) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(URL + url)
                .addHeader("Authorization", token)
                .get()
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    then.then(response.body().string());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public static void post(String url, String gson, Then then) {
        Log.d(TAG, "post: " + gson);
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), gson);
        Request request = new Request.Builder()
                .url(URL + url)
                .addHeader("Authorization", token)
                .post(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    then.then(response.body().string());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void delete(String url, String gson, Then then) {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), gson);
        Request request = new Request.Builder()
                .url(URL + url)
                .addHeader("Authorization", token)
                .delete(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    then.then(response.body().string());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public static void put(String url, String gson, Then then) {
        Log.d(TAG, "put: "+gson);
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), gson);
        Request request = new Request.Builder()
                .url(URL + url)
                .addHeader("Authorization", token)
                .put(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    then.then(response.body().string());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void put(String url, Then then) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(URL + url)
                .addHeader("Authorization", token)
                .put(null)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    then.then(response.body().string());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public static void postFile(String url, String path, Then then) {
        OkHttpClient client = new OkHttpClient();
        File file = new File(path);
        RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody multipartBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("mFile", file.getName(), body)
                .build();
        Request request = new Request.Builder()
                .url(URL + url)
                .addHeader("Authorization", token)
                .post(multipartBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    then.then(response.body().string());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void uploadFiles(String url, List<String> paths, Then then) {
        OkHttpClient client = new OkHttpClient();
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        for (String path : paths) {
            File file = new File(path);
            builder.addFormDataPart("files", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
        }
        Request request = new Request.Builder()
                .url(URL + url)
                .addHeader("Authorization", token)
                .post(builder.build())
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    then.then(response.body().string());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
